﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using KVB.Common;

namespace Customer_Profile.ClientManage
{
    public class CustomerDao
    {
        SqlConnection conn = null;
        public CustomerDao()
		{}

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(Customer model, string operatename)
        {
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = {
					new SqlParameter("@PhonePassword", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@FirstName", SqlDbType.NVarChar,50),
					new SqlParameter("@LastName", SqlDbType.NVarChar,50),
					new SqlParameter("@HomeNo", SqlDbType.NVarChar,50),
					new SqlParameter("@WorkNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FaxNo", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,100),
					new SqlParameter("@FINNo", SqlDbType.NVarChar,50),
					new SqlParameter("@CSNNo", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,50),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@RegionProvince", SqlDbType.NVarChar,50),
					new SqlParameter("@Country", SqlDbType.NVarChar,50),
					new SqlParameter("@PostalCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Remarks", SqlDbType.NVarChar,500),
					new SqlParameter("@Advisor", SqlDbType.NVarChar,50),
					new SqlParameter("@Birthday", SqlDbType.NVarChar,50),
					new SqlParameter("@Status", SqlDbType.NVarChar,50),
					new SqlParameter("@Location", SqlDbType.NVarChar,50),
					new SqlParameter("@IBID", SqlDbType.NVarChar,50),
					new SqlParameter("@IBLoginName", SqlDbType.NVarChar,50),
                    new SqlParameter("@operator",SqlDbType.NVarChar,50),                    
                    new SqlParameter("@A_FirstName",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_LastName",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_HomeNo",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_WorkNO",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Birth",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Address",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Country",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_City",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Province",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Title",SqlDbType.NVarChar,50)
                };
                parameters[0].Value = model.PhonePassword;
                parameters[1].Value = model.Title;
                parameters[2].Value = model.FirstName;
                parameters[3].Value = model.LastName;
                parameters[4].Value = model.HomeNo;
                parameters[5].Value = model.WorkNo;
                parameters[6].Value = model.FaxNo;
                parameters[7].Value = model.Email;
                parameters[8].Value = model.FINNo;
                parameters[9].Value = model.CSNNo;
                parameters[10].Value = model.Address;
                parameters[11].Value = model.City;
                parameters[12].Value = model.RegionProvince;
                parameters[13].Value = model.Country;
                parameters[14].Value = model.PostalCode;
                parameters[15].Value = model.Remarks;
                parameters[16].Value = model.Advisor;
                parameters[17].Value = model.Birthday;
                parameters[18].Value = model.Status;
                parameters[19].Value = model.Location;
                parameters[20].Value = model.IBID;
                parameters[21].Value = model.IBLoginName;
                parameters[22].Value = operatename;
                parameters[23].Value = model.authorise_FirstName;
                parameters[24].Value = model.authorise_LastName;
                parameters[25].Value = model.authorise_HomeNO;
                parameters[26].Value = model.authorise_WorkNO;
                parameters[27].Value = model.authorise_Birth;
                parameters[28].Value = model.authorise_Address;
                parameters[29].Value = model.authorise_Country;
                parameters[30].Value = model.authorise_City;
                parameters[31].Value = model.authorise_Province;
                parameters[32].Value = model.authorise_Title;

                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Proc_TP_TCustomer_ADD", parameters);                
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("Proc_TP_TCustomer_ADD", ex.ToString());
                throw ex;
            }
            finally
            {
                DB.ConnDispose(ref conn);
            }
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(Customer model, string operatename)
        {
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = {
					new SqlParameter("@Guid", SqlDbType.NVarChar,50),
					new SqlParameter("@PhonePassword", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,50),
					new SqlParameter("@FirstName", SqlDbType.NVarChar,50),
					new SqlParameter("@LastName", SqlDbType.NVarChar,50),
					new SqlParameter("@HomeNo", SqlDbType.NVarChar,50),
					new SqlParameter("@WorkNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FaxNo", SqlDbType.NVarChar,50),
					new SqlParameter("@Email", SqlDbType.NVarChar,100),
					new SqlParameter("@FINNo", SqlDbType.NVarChar,50),
					new SqlParameter("@CSNNo", SqlDbType.NVarChar,50),
					new SqlParameter("@Address", SqlDbType.NVarChar,50),
					new SqlParameter("@City", SqlDbType.NVarChar,50),
					new SqlParameter("@RegionProvince", SqlDbType.NVarChar,50),
					new SqlParameter("@Country", SqlDbType.NVarChar,50),
					new SqlParameter("@PostalCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Remarks", SqlDbType.NVarChar,500),
					new SqlParameter("@Advisor", SqlDbType.NVarChar,50),
					new SqlParameter("@Birthday", SqlDbType.NVarChar,50),
					new SqlParameter("@Status", SqlDbType.NVarChar,50),
					new SqlParameter("@Location", SqlDbType.NVarChar,50),
					new SqlParameter("@IBID", SqlDbType.NVarChar,50),
					new SqlParameter("@IBLoginName", SqlDbType.NVarChar,50),
                    new SqlParameter("@operator",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_FirstName",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_LastName",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_HomeNo",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_WorkNO",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Birth",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Address",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Country",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_City",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Province",SqlDbType.NVarChar,50),
                    new SqlParameter("@A_Title",SqlDbType.NVarChar,50)
                };
                parameters[0].Value = model.Guid;
                parameters[1].Value = model.PhonePassword;
                parameters[2].Value = model.Title;
                parameters[3].Value = model.FirstName;
                parameters[4].Value = model.LastName;
                parameters[5].Value = model.HomeNo;
                parameters[6].Value = model.WorkNo;
                parameters[7].Value = model.FaxNo;
                parameters[8].Value = model.Email;
                parameters[9].Value = model.FINNo;
                parameters[10].Value = model.CSNNo;
                parameters[11].Value = model.Address;
                parameters[12].Value = model.City;
                parameters[13].Value = model.RegionProvince;
                parameters[14].Value = model.Country;
                parameters[15].Value = model.PostalCode;
                parameters[16].Value = model.Remarks;
                parameters[17].Value = model.Advisor;
                parameters[18].Value = model.Birthday;
                parameters[19].Value = model.Status;
                parameters[20].Value = model.Location;
                parameters[21].Value = model.IBID;
                parameters[22].Value = model.IBLoginName;
                parameters[23].Value = operatename;
                parameters[24].Value = model.authorise_FirstName;
                parameters[25].Value = model.authorise_LastName;
                parameters[26].Value = model.authorise_HomeNO;
                parameters[27].Value = model.authorise_WorkNO;
                parameters[28].Value = model.authorise_Birth;
                parameters[29].Value = model.authorise_Address;
                parameters[30].Value = model.authorise_Country;
                parameters[31].Value = model.authorise_City;
                parameters[32].Value = model.authorise_Province;
                parameters[33].Value = model.authorise_Title;
                DB.ExecuteNonQuery(conn, CommandType.StoredProcedure, "Proc_TP_TCustomer_Update", parameters);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("Proc_TP_TCustomer_Update", ex.ToString());
                throw ex;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Customer GetModel(string Guid)
        {
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parameters = {
					new SqlParameter("@Guid", SqlDbType.NVarChar,50)};
                parameters[0].Value = Guid;

                Customer model = new Customer();
                DataSet ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "Proc_TP_TCustomer_GetModel", parameters);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    model.Guid = ds.Tables[0].Rows[0]["Guid"].ToString();
                    model.PhonePassword = ds.Tables[0].Rows[0]["PhonePassword"].ToString();
                    model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                    model.FirstName = ds.Tables[0].Rows[0]["FirstName"].ToString();
                    model.LastName = ds.Tables[0].Rows[0]["LastName"].ToString();
                    model.HomeNo = ds.Tables[0].Rows[0]["HomeNo"].ToString();
                    model.WorkNo = ds.Tables[0].Rows[0]["WorkNo"].ToString();
                    model.FaxNo = ds.Tables[0].Rows[0]["FaxNo"].ToString();
                    model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                    model.FINNo = ds.Tables[0].Rows[0]["FINNo"].ToString();
                    model.CSNNo = ds.Tables[0].Rows[0]["CSNNo"].ToString();
                    if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                    {
                        model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                    }
                    model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                    model.City = ds.Tables[0].Rows[0]["City"].ToString();
                    model.RegionProvince = ds.Tables[0].Rows[0]["RegionProvince"].ToString();
                    model.Country = ds.Tables[0].Rows[0]["Country"].ToString();
                    model.PostalCode = ds.Tables[0].Rows[0]["PostalCode"].ToString();
                    model.Remarks = ds.Tables[0].Rows[0]["Remarks"].ToString();
                    if (ds.Tables[0].Rows[0]["ModifyDate"].ToString() != "")
                    {
                        model.ModifyDate = DateTime.Parse(ds.Tables[0].Rows[0]["ModifyDate"].ToString());
                    }
                    model.Advisor = ds.Tables[0].Rows[0]["Advisor"].ToString();
                    model.Birthday = ds.Tables[0].Rows[0]["Birthday"].ToString();
                    model.Status = ds.Tables[0].Rows[0]["Status"].ToString();
                    model.Location = ds.Tables[0].Rows[0]["Location"].ToString();
                    model.IBID = ds.Tables[0].Rows[0]["IBID"].ToString();
                    model.IBLoginName = ds.Tables[0].Rows[0]["IBLoginName"].ToString();
                    model.authorise_Title = ds.Tables[0].Rows[0]["authorise_Title"].ToString();
                    model.authorise_FirstName = ds.Tables[0].Rows[0]["authorise_FirstName"].ToString();
                    model.authorise_LastName = ds.Tables[0].Rows[0]["authorise_LastName"].ToString();
                    model.authorise_HomeNO = ds.Tables[0].Rows[0]["authorise_HomeNO"].ToString();
                    model.authorise_WorkNO = ds.Tables[0].Rows[0]["authorise_WorkNO"].ToString();
                    model.authorise_Birth = ds.Tables[0].Rows[0]["authorise_Birth"].ToString();
                    model.authorise_Address = ds.Tables[0].Rows[0]["authorise_Address"].ToString();
                    model.authorise_City = ds.Tables[0].Rows[0]["authorise_City"].ToString();
                    model.authorise_Province = ds.Tables[0].Rows[0]["authorise_Province"].ToString();
                    model.authorise_Country = ds.Tables[0].Rows[0]["authorise_Country"].ToString();
                    return model;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("Proc_TP_TCustomer_GetModel", ex.ToString());
                return null;
            }
        }

        internal DataTable GetListByCondition(string where)
        {
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parms ={ new SqlParameter("@where", SqlDbType.NVarChar, 3500) };
                parms[0].Value = where;
                DataSet ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "Proc_TP_TCustomer_GetList", parms);
                if (ds != null & ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else 
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("GetListByCondition", ex.ToString());
                return null;
            }
        }

        internal DataTable GetListByConditionView(string where)
        {
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parms ={ new SqlParameter("@where", SqlDbType.NVarChar, 3500) };
                parms[0].Value = where;
                DataSet ds = DB.ExecuteDataset(conn, CommandType.StoredProcedure, "Proc_TP_TCustomer_GetListView", parms);
                if (ds != null & ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("GetListByCondition", ex.ToString());
                return null;
            }
        }

        internal void ChangeStatus(string guid, string operatename)
        {
            try
            {
                DB.Connection(ref conn);
                SqlParameter[] parms ={ new SqlParameter("@Guid", SqlDbType.NVarChar, 50),
                        new SqlParameter("@operator",SqlDbType.NVarChar,50)};
                parms[0].Value = guid;
                parms[1].Value = operatename;
                DB.ExecuteNonQuery(conn,CommandType.StoredProcedure,"Proc_TP_TChangeStatus",parms);
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("ChangeStatus", ex.ToString());
            }
        }
    }
}
